package tree.ThreadedBinaryTree;

public class ThreadedBinaryTree {
}
class HeroNode {
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;
    private int leftType;// ==0 表示指向的是左子树 ==1 表示指向前驱节点

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    private int rightType;
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 遍历方法
     */
    public void preOrder() {
        System.out.println(this);//this 就是自己 输出父节点
        //递归向左子树前序遍历
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.preOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    public void postOrder() {
        if (this.left != null) {
            this.left.postOrder();
        }
        if (this.right != null) {
            this.right.postOrder();
        }
        System.out.println(this);
    }

    public HeroNode preOrderSearch(int no) {
        System.out.println("进入preOrderSearch");
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) {
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    public HeroNode infixOrderSearch(int no) {
        HeroNode reNode = null;
        if (this.left != null) {
            reNode = this.left.infixOrderSearch(no);
        }
        if (reNode != null) {
            return reNode;
        }
        System.out.println("进入infix查找");
        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            reNode = this.right.infixOrderSearch(no);
        }
        return reNode;
    }

    public HeroNode postOrderSearch(int no) {
        HeroNode resNode = null;
        if (this.left != null) {
            resNode= this.left.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right!=null){
            resNode = this.right.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("进入post查找");
        if (this.no==no){
            return this;
        }
        return  resNode;
    }
    /**
     * 删除节点
     * 叶子 删除节点
     * 非叶子节点 删除子树
     */
    public void delNode(int no){
        if (this.left!=null&&this.left.no==no){
            this.left=null;
            return;
        }
        if (this.right!=null&&this.right.no==no){
            this.right=null;
            return;
        }
        if (this.left!=null){
            this.left.delNode(no);
            return;
        }
        if (this.right!=null){
            this.right.delNode(no);
            return;
        }

    }

}
class BinaryTree {
    private HeroNode root;
    private HeroNode pre=null;//为了实现线索化，需要创建要给指向当前节点的前驱节点的指针
    //编写对二叉树进行中序线索化的方法

    /**
     *
     * @param node 就是当前需要线索化的节点
     */
    public void threadedNodes(HeroNode node){
        //如果node==null不能线索化
        if (node==null){
            return;
        }
        //先线索化左子树
        threadedNodes(node.getLeft());
        //线索化当前节点
        if (node.getLeft()==null){
            //让结点的左指针指向前驱结点
            node.setLeft(pre);
            node.setLeftType(1);// ==1 表示指向前驱节点 0子树
        }
        //每处理一个结点后，让当前节点是下一个节点的前驱结点
        pre=node;
        //线索化右子树
        threadedNodes(node.getRight());
    }

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为空");
        }
    }

    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为空");
        }
    }

    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为空");
        }
    }

    public HeroNode preOrderSearch(int no) {
        if (root != null) {
            return root.preOrderSearch(no);
        } else {
            return null;
        }
    }

    public HeroNode postOrderSearch(int no) {
        if (root!=null){
            return root.postOrderSearch(no);
        }else  {
            System.out.println("树为空");
            return null;
        }
    }

    public HeroNode infixOrderSearch(int no) {
        if (root!=null){
            return root.infixOrderSearch(no);
        }else  {
            System.out.println("树为空");
            return null;
        }
    }
    public void delNode(int no){
        if (root!=null){
            //先判断是不是这个就是要删除的节点
            if (root.getNo()==no){
                root=null;
            }else {
                root.delNode(no);
            }
        }else {
            System.out.println("空树，无法删除");
        }
    }
    public void threadedList(){
        //定义一个变量，储存当前节点 ，从root开始
        HeroNode curNode=root;
        while (curNode!=null){
            while (curNode.getLeftType()==0){
                curNode=curNode.getLeft();
            }
            System.out.println(curNode);
            while (curNode.getRightType()==1){
                curNode=curNode.getRight();
                System.out.println(curNode);
            }
            curNode=curNode.getRight();
            
        }
    }
}